package com.agritech.state;

import com.agritech.machine.AgriculturalMachineContext;

public class WorkingState implements MachineState {
    @Override
    public void start(AgriculturalMachineContext machine) {
        System.out.println("农机 [" + machine.getMachineId() + "] 正在作业中，无需重复启动。");
    }

    @Override
    public void stop(AgriculturalMachineContext machine) {
        System.out.println("农机 [" + machine.getMachineId() + "] 停止当前作业并返回运行状态...");
        machine.setState(new RunningState());
    }

    @Override
    public void work(AgriculturalMachineContext machine) {
        if (machine.getFuelLevel() < 5) {
            System.out.println("农机 [" + machine.getMachineId() + "] 作业中油量严重不足 (" + String.format("%.1f", machine.getFuelLevel()) + "%)，即将自动停止作业!");
            machine.stop(); // 自动停止
            return;
        }
        System.out.println("农机 [" + machine.getMachineId() + "] 正在努力作业... (" + machine.getType() + ")");
        // 模拟作业消耗油量
        machine.setFuelLevel(machine.getFuelLevel() - 10);
        if (machine.getFuelLevel() <= 0) {
            System.out.println("农机 [" + machine.getMachineId() + "] 油料耗尽，作业被迫中止!");
            machine.setState(new OutOfFuelState());
        }
    }

    @Override
    public void fix(AgriculturalMachineContext machine) {
        System.out.println("农机 [" + machine.getMachineId() + "] 正在作业，无法进行检修，请先停止作业。");
    }

    @Override
    public String getStateName() {
        return "作业中";
    }
}